package com.sxh.user.Utils;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
//获取用户IP(参考AI)
public class IPUtil {
    public static String getClientIp() {
        ServletRequestAttributes attributes = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return "unknown";
        }
        HttpServletRequest request = attributes.getRequest();
        String xffHeader = request.getHeader("X-Forwarded-For");
        return xffHeader == null ? request.getRemoteAddr() : xffHeader.split(",")[0];
    }
}